﻿using System.Collections.Generic;
using System.Linq;
using Realms;
using Zenject;

public class BaseMapper<T> where T : RealmObject
{
    [Inject] private RealmDataSource _realmDataSource;

    public void InsertSingle(T data)
    {
        _realmDataSource.realmInstance.Write(() =>
        {
            // 添加一条 Player 数据
            _realmDataSource.realmInstance.Add(data);
        });
    }

    public void InsertDual(IEnumerable<T> hero)
    {
        foreach (T item in hero)
        {
            _realmDataSource.realmInstance.Write(() =>
            {
                // 添加一条 Player 数据
                _realmDataSource.realmInstance.Add(item);
            });
        }
    }

    public T SelectById(long id)
    {
        return _realmDataSource.realmInstance.Find<T>(id);
    }

    public IQueryable<T> SelectAll()
    {
        return _realmDataSource.realmInstance.All<T>();
    }

    public void DeleteById(int id)
    {
        _realmDataSource.realmInstance.Remove(SelectById(id));
    }
}